Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Java 8 Interview Questions and Answers

Question: Can you explain the syntax of Lambda expression?
Answer:
So we can divide structure of Lambda expression to three parts:
  • Arguments
  • Array Token
  • Statements
1. Argument list or parameters
Lambda expression can have zero or more arguments. First part before '->' is called as argument list or parameters.
()->{System.out.println("Hello")}; //Without argument, will print hello 
(int a)->{System.out.println(a)} //; One argument, will print value of a
(int a,int b)-> {a+b};//two argument, will return sum of these two integers

2. Array token (->)

3. Body
  • Body can have expression or statements.
  • If there is only one statement in body, curly brace is not needed and return type of the anonymous function is same as of  body expression.
  • If there are more than one statements, then it should be in curly braces and return type of anonymous function is same as value return from code block, void if nothing is returned.
Is it helpful? Yes No

Most helpful rated by users:

©2024 WithoutBook